package com.example.myandroidactualcombat.ui.dashboard;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.Adapter.BookAdapter;
import com.Adapter.CollectionAdapter;
import com.example.myandroidactualcombat.BooksActivity;
import com.example.myandroidactualcombat.ChooseActivity;
import com.example.myandroidactualcombat.R;
import com.example.myandroidactualcombat.ReadActivity;
import com.example.myandroidactualcombat.data.Book;
import com.example.myandroidactualcombat.data.ChooseBook;
import com.example.myandroidactualcombat.data.Collection;
import com.example.myandroidactualcombat.ui.home.HomeFragment;
import com.example.myandroidactualcombat.ui.home.HomeViewModel;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class DashboardFragment extends Fragment {
    List<ChooseBook>chooseBookList;
    List<Book>bookList;
    List<Book>datalist;
    List<Book>extar;
    BookAdapter adapter;
    int userid;
    int bookid;


    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        initView(root);
        addListener(root);
        return root;
    }

    private void addListener(final View root) {
        final ViewHolder viewHolder=new ViewHolder(root);
        viewHolder.mLvSearch.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                bookid=datalist.get(i).getId();
                int i1=LitePal.deleteAll(ChooseBook.class,"userid=? and bookid=?",userid+"",bookid+"");
                if (i1!=0){
                    datalist.remove(i);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(getContext(), "删除收藏成功", Toast.LENGTH_SHORT).show();
                    initView(root);
                    viewHolder.mEdSearch.setText("");
                }else{
                    Toast.makeText(getContext(), "删除收藏失败", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });
        viewHolder.mBuSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (viewHolder.mEdSearch.getText().toString().trim().equals("")){
                    Log.i("TAG", "onClick"+extar.size()+"");
                    datalist.clear();
                    Log.i("TAG", "onClick"+extar.size()+"");
                    datalist.addAll(extar);
                }else{
                    getSearchReult(root);
                }
                adapter.notifyDataSetChanged();
            }
        });
        viewHolder.mLvSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getContext(), ReadActivity.class);
                intent.putExtra("bookname",bookList.get(i).getBookname());
                startActivity(intent);
            }
        });
    }
    List<Book> searchlist;
    private void getSearchReult(View root) {
        final ViewHolder viewHolder=new ViewHolder(root);
        searchlist =new ArrayList<>();
        for (int i=0;i<datalist.size();i++){
            if (datalist.get(i).getBookname().contains(viewHolder.mEdSearch.getText().toString().trim())){
                searchlist.add(datalist.get(i));
            }
        }
        datalist.clear();
        datalist.addAll(searchlist);
    }

    private void initView(View root) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
        userid=prefs.getInt("userid",0);
        chooseBookList= LitePal.where("userid=?",userid+"").find(ChooseBook.class);
        ArrayList<Integer> ids = new ArrayList<>();
        for (int i=0;i<chooseBookList.size();i++){
            ids.add(chooseBookList.get(i).getBookid());
        }
        bookList=LitePal.findAll(Book.class);
        datalist=new ArrayList<>();
        for (int j=0;j<ids.size();j++){
            for (int k=0;k<bookList.size();k++){
                Book book = bookList.get(k);
                if (ids.get(j)==book.getId()){
                    datalist.add(book);
                }
            }
        }
        extar=new ArrayList<>();
        extar.addAll(datalist);
        adapter=new BookAdapter(getContext(),R.layout.book_item,datalist);
        ViewHolder viewHolder=new ViewHolder(root);
        viewHolder.mLvSearch.setAdapter(adapter);
    }

    public static class ViewHolder {
        public View rootView;
        public EditText mEdSearch;
        public Button mBuSearch;
        public ListView mLvSearch;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.mEdSearch = (EditText) rootView.findViewById(R.id.ed_search);
            this.mBuSearch = (Button) rootView.findViewById(R.id.bu_search);
            this.mLvSearch = (ListView) rootView.findViewById(R.id.lv_search);
        }
    }
}